/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Sep 6, 2004
 *
 */
package jiggle.relo;

import jiggle.AbstractForceLaw;
import jiggle.Cell;
import jiggle.Edge;
import jiggle.Graph;

/**
 * This ForceLaw essentially models an attraction force in the target dimension
 * for nodes on the wrong side.
 *  
 * @author vineet
 *  
 */
public class HardPointedForceLaw extends AbstractForceLaw {

    AbstractForceLaw attrLaw;

    public HardPointedForceLaw(Graph g, double k, AbstractForceLaw attrLaw, AbstractForceLaw repulseLaw) {
        super(g, k);
        this.attrLaw = attrLaw;
    }

	@Override
    public double pairwiseWeights(double pref, double curr) {
		return attrLaw.pairwiseWeights(pref,curr);
	}


	@Override
    public void applyForCells(double[][] negativeGradient, Edge e) {
        if (!(e instanceof PointedEdge)) {
            return;
        }
        PointedEdge pe = (PointedEdge) e;
        
		Cell from = e.getFrom();
		Cell to = e.getTo();

        int tgtDim = pe.getDim();

        double[] fromCoords = from.getCoords();
        double[] toCoords = to.getCoords();

        // only apply force if direction is invalid
        double prefToPos = fromCoords[tgtDim] 
				+ from.getSize()[tgtDim]
				+ to.getSize()[tgtDim]
				+ (preferredEdgeLength * pe.getIncrFactor());
        
        // hard solution A: swap positions [get's tricky because you need to 
        //  determine uner what cases do you wnat to swap]
        /*
        if ( (fromCoords[tgtDim] - toCoords[tgtDim]) * pe.dimensionalDir > 0) {
	        double[] fromCoords_c = (double[]) fromCoords.clone();
	        double[] toCoords_c = (double[]) toCoords.clone();
	        
	        from.setCoords(toCoords_c);
	        to.setCoords(fromCoords_c);
        }
        */
        
        // hard solution B: move both from an to coors in the direction needed
        double moveDst = prefToPos - toCoords[tgtDim];
        // only move if in the overlap scenario
        if (moveDst > preferredEdgeLength) {
	        toCoords[tgtDim] += moveDst/2;
	        fromCoords[tgtDim] -= moveDst/2;
	        from.recomputeBoundaries();
	        to.recomputeBoundaries();
	
	        System.err.println("Hard Move: [" + from.gradientNdx + ", " + to.gradientNdx + "] Distance: " + moveDst);
        } else if (moveDst > 0) {
            // TODO: apply some force here (so that it doesn't get to the above situation)
            //  vertex vertex repulsion combined with spring law should do the trick though 
        }
        
    }


}